#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDir>
#include <QTreeWidget>
#include <QString>
#include <QTreeWidgetItem>
#include <QFileDialog>
#include <QDebug>
#include <QStringList>
#include <QImageReader>
#include <QImage>

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{

    ui->setupUi(this);
    treeFile=new QTreeWidget(this);

    centerSplitter=new QSplitter(Qt::Vertical);
    imgOrigin=new QLabel("原始图片",this);

    imgProcessed=new QLabel("处理后的图片",this);

    centerSplitter->addWidget(imgOrigin);
    centerSplitter->addWidget(imgProcessed);
    centerSplitter->setStretchFactor(1,1);


    rightSplitter=new QSplitter(Qt::Vertical);
    mainSplitter =new QSplitter(Qt::Horizontal);

    mainSplitter->addWidget(treeFile);
    mainSplitter->addWidget(centerSplitter);
    mainSplitter->addWidget(rightSplitter);
    mainSplitter->setStretchFactor(1,1);

    setCentralWidget(mainSplitter);

    setWindowTitle("workbench of image process");

    QObject::connect(ui->actionOpen_O,SIGNAL(triggered()),this,SLOT(initFileTree()));
    QObject::connect(treeFile,SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),this,SLOT(showSelectedImg(QTreeWidgetItem*,int)));

}
void MainWindow::initFileTree()
{
    QString folder=QFileDialog::getExistingDirectory();
    _folderName=folder;
    if(folder.isEmpty())return;
    CreateFileTree(treeFile,folder);

}

void MainWindow::showSelectedImg(QTreeWidgetItem* item,int n)
{
    QString name=item->text(0);
    QString path= _folderName+"/"+name;

    QStringList filters;
    foreach(QByteArray format,QImageReader::supportedImageFormats())
    filters.append(format);

    if(!path.contains('.')) return;

    QStringList list=path.split('.');
    QString extentName=list.at(list.count()-1);
    if(!filters.contains(extentName)) return;


    Mat imgMat=cv::imread(path.toLatin1().data());
    QImage qImage;

    if(imgMat.channels()==3)
    {
        cv::cvtColor(imgMat,imgMat,CV_BGR2RGB);
    }
    qImage=QImage((const unsigned char *)(imgMat.data),
                  imgMat.cols,imgMat.rows, imgMat.cols*imgMat.channels(),
                  QImage::Format_RGB888
                  );
    imgOrigin->setPixmap(QPixmap::fromImage(qImage));
    imgOrigin->resize(imgOrigin->pixmap()->size());

}

//create file tree where given tree and folder.
void MainWindow::CreateFileTree(QTreeWidget *tree,QString folder)
{
    if(folder.isEmpty())return;

    tree->setColumnCount(1);
    tree->setHeaderLabel(tr(""));
    tree->setHeaderLabel(tr("imageSelect"));

    QDir dirPath(_folderName);
    if(!dirPath.exists())return;

    //image filter
    dirPath.setFilter(QDir::NoSymLinks|QDir::Dirs|QDir::Files);

    foreach(QFileInfo mfi,dirPath.entryInfoList())
    {
        if(mfi.isFile())
        {
            QTreeWidgetItem *imageItem=new QTreeWidgetItem(tree);
            imageItem->setText(0,mfi.fileName());
            imageItem->setIcon(0,QIcon(":/image/mainWindow/file.jpg"));
        }
        if(mfi.isDir())
        {
            QTreeWidgetItem *imageItem=new QTreeWidgetItem(tree);
            imageItem->setText(0,mfi.fileName());
            imageItem->setIcon(0,QIcon(":/image/mainWindow/folder.jpg"));
        }
    }


}



MainWindow::~MainWindow()
{
    delete ui;
}
